package kr.ac.postech.marathon.client.widgets;


import kr.ac.postech.marathon.client.HasModel;
import kr.ac.postech.marathon.client.models.Registration;


import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextBox;

public class RegistrationForm extends Composite implements HasModel<Registration> {
	
	private static final String[] TYPES = {
			"일반부(45세 이하)",
			"장년부(46세 이상)",
			"여자부",
			"POSTECH 학생부 (남)",
			"POSTECH 학생부 (여)"
	};
	
	private HorizontalPanel typePanel;
	private TextBox dept;
	private TextBox name;
	private IdNumberForm id;
	private AddressForm addr;
	
	
	public RegistrationForm() {
		FlexTable t = new FlexTable();
		t.setText(0, 0, "신청구분");
		t.setText(1, 0, "소속(학과)");
		t.setText(2, 0, "성명");
		t.setText(3, 0, "주민등록 번호");
		t.setText(4, 0, "주소");
		t.setText(5, 0, "연락처");
		t.setText(6, 0, "티셔츠 사이즈");
		t.setText(7, 0, "입금계좌번호");
		
		// Type of registration
		typePanel = new HorizontalPanel();
		for(String type : TYPES) {
			typePanel.add(new RadioButton("type", type));
		}
		t.setWidget(0, 1, typePanel);
		
		// Department
		dept = new TextBox();
		t.setWidget(1, 1, dept);
		
		// Name
		name = new TextBox();
		t.setWidget(2, 1, name);
		
		// Id number
		id = new IdNumberForm();
		t.setWidget(3, 1, id);
		
		// Address
		addr = new AddressForm();
		t.setWidget(4, 1, addr);
		
	
		
		initWidget(t);
	}

	
	public Registration getModel() {
		Registration r = new Registration();
		
		// Type recognition
		String type = "";
		for(int i = 0; i < typePanel.getWidgetCount(); i++) {
			RadioButton b = (RadioButton)typePanel.getWidget(i);
			if(b.getValue()){
				type = b.getText();
			}
		}
		r.setType(type);
		
		r.setDept(dept.getText());
		r.setName(name.getText());
		r.setId(id.getModel());
		r.setAddr(addr.getModel());
		
		return r;
	}
}